﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using LGE;

namespace MagicFight
{
    class Beam : GameObject
    {
        int interval = Def.ArcaneBeamDef.Interval; // intervalo de tempo que um alvo tem q ficar no raio para receber dano
        int damage = Def.ArcaneBeamDef.Damage; // dano que um alvo recebe a cada intervalo

        Character caster;

        Dictionary<Character, double> targets = new Dictionary<Character, double>(); // alvos que foram afetados pelo raio
        List<Vector2> path = new List<Vector2>(); // lista que armazena todas as posições pelas quais o raio passa
        int spacing = 30; // espaçamento entre cada posição que o raio passa
        Texture2D beam_start = Engine.Load<Texture2D>("Images\\Magic\\ArcaneBeam\\beam_start");
        Texture2D beam_body = Engine.Load<Texture2D>("Images\\Magic\\ArcaneBeam\\beam_body");
        Texture2D beam_end = Engine.Load<Texture2D>("Images\\Magic\\ArcaneBeam\\beam_end");
        

        public Beam(Character caster)
            : base(Engine.Load<Texture2D>("Images\\Magic\\ArcaneBeam\\beam_body"))
        {
            this.caster = caster;
            position = caster.position + direction * 40;
            direction = caster.direction;
        }

        public override void Update(GameTime gameTime)
        {
            if (!alive) return;
            direction = caster.direction;
            position = caster.position + direction * 40;
            UpdatePath();
            CheckCollision(gameTime);
        }

        private void UpdatePath()
        {
            path.Clear();
            path.Add(position);
            Vector2 nextPos = position + spacing * direction;
            while (nextPos.X > 100 && nextPos.X < 1180 && nextPos.Y > 100 & nextPos.Y < 620)
            {
                path.Add(nextPos);
                nextPos = nextPos + spacing * direction;
            }
            path.Add(nextPos);
        }

        private void CheckCollision(GameTime gameTime)
        {
            foreach (GameObject obj in ps.gameObjects)
            {
                if (obj == null) continue;
                if (!obj.alive) continue;
                if (obj == this) continue;
                if (obj == caster) continue;
                if (Collision(obj))
                {
                    if (obj is Projectile) (obj as Projectile).collided = true;
                    if (obj is Character)
                    {
                        Character c = (obj as Character);
                        if (targets.ContainsKey(c)) targets[c] += gameTime.ElapsedGameTime.TotalMilliseconds;
                        else targets.Add(c, gameTime.ElapsedGameTime.TotalMilliseconds);
                        if (targets[c] > interval)
                        {
                            targets[c] -= interval;
                            int d = c.TakeDamage(damage, caster);
                            if (d > 0) ps.effectsManager.Add(new FloatingText(c.position, d.ToString(), Color.Red));
                        }
                    }
                }
            }
            //foreach (KeyValuePair<Character, double> x in targets) if (x.Value > interval) { x.Value -= interval; x.Key.TakeDamage(damage); }
        }

        public override bool Collision(EngineObject target)
        {
            foreach (Vector2 p in path) if (Vector2.Distance(p, target.position) < (this.radius + target.radius)) return true;
            return false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (!alive) return;
            foreach (Vector2 p in path)
                if (path.IndexOf(p) == 0) spriteBatch.Draw(beam_start, p, null, Color.White, rotation, origin, 1, SpriteEffects.None, 0);
                else if (path.IndexOf(p) == path.Count - 1) spriteBatch.Draw(beam_end, p, null, Color.White, rotation, origin, 1, SpriteEffects.None, 0);
                else spriteBatch.Draw(beam_body, p, null, Color.White, rotation, origin, 1, SpriteEffects.None, 0);
        }

        public override void MapCollision(Vector2 dir)
        {
        }
    }
}
